//
//  RBTabbarController.m
//  Budejie
//
//  Created by 华杨科技 on 2017/9/17.
//  Copyright © 2017年 饶波. All rights reserved.
//

#import "RBTabbarController.h"
#import "RBTabBar.h"
#import "RBEssenceViewController.h"
#import "RBFollowViewController.h"
#import "RBNewViewController.h"
#import "RBMeViewController.h"
#import "RBNavigationController.h"


@implementation RBTabbarController
#pragma mark - 初始化
// view 刚刚创建完
- (void)viewDidLoad {
    [super viewDidLoad];
    
    /**设置所有tabbarItem的文字属性**/
    [self setupItemTitleTextAttribute];
    
    /**添加控制器**/
    [self setupSubController];
    
    /*** 更换tabBar***/
    [self setValue:[[RBTabBar alloc] init] forKey:@"tabBar"];    
}

- (void)setupItemTitleTextAttribute {
    UITabBarItem *item = [UITabBarItem appearance]; // 只要有这个标志 UI_APPEARANCE_SELECTOR 都可以统一设置 
    
    // 设置普通状态下文字属性
    NSMutableDictionary *normatAttrs = [NSMutableDictionary dictionary];
    normatAttrs[NSFontAttributeName] = [UIFont systemFontOfSize:12];
    normatAttrs[NSForegroundColorAttributeName] = [UIColor lightGrayColor];
    [item setTitleTextAttributes:normatAttrs forState:UIControlStateNormal];
    
    // 选中状态下文字属性
    NSMutableDictionary *selectAttrs = [NSMutableDictionary dictionary];
    selectAttrs[NSFontAttributeName] = [UIFont systemFontOfSize:12];
    selectAttrs[NSForegroundColorAttributeName] = [UIColor darkGrayColor];
    [item setTitleTextAttributes:selectAttrs forState:UIControlStateSelected];
}

- (void)setupSubController {
    
    [self setupOneChildViewControler:[[RBNavigationController alloc] initWithRootViewController:[[RBEssenceViewController alloc] init]] title:@"精华" image:@"tabBar_essence_icon" selectedImage:@"tabBar_essence_click_icon"];
    [self setupOneChildViewControler:[[RBNavigationController alloc] initWithRootViewController:[[RBMeViewController alloc] init]] title:@"我" image:@"tabBar_new_icon" selectedImage:@"tabBar_new_click_icon"];    
    [self setupOneChildViewControler:[[RBNavigationController alloc] initWithRootViewController:[[RBNewViewController alloc] init]] title:@"新帖" image:@"tabBar_new_icon" selectedImage:@"tabBar_new_click_icon"];
    RBFollowViewController *followVC = [[RBFollowViewController alloc] initWithNibName:@"RBFollowViewController" bundle:nil];
    [self setupOneChildViewControler:[[RBNavigationController alloc] initWithRootViewController:followVC] title:@"关注" image:@"tabBar_friendTrends_icon" selectedImage:@"tabBar_friendTrends_click_icon"];
    
}

/**
 初始化一个控制器

 @param vc 子控制器
 @param title 标题
 @param image 图标
 @param selectedImage 选中图标
 */
- (void)setupOneChildViewControler:(UIViewController *)vc title:(NSString *)title image:(NSString *)image selectedImage:(NSString *)selectedImage
{
//    vc.view.backgroundColor = RBRandomColor;
    vc.tabBarItem.title = title;
    
    if (image.length) {
        vc.tabBarItem.image = [UIImage imageNamed:image];
        vc.tabBarItem.selectedImage = [UIImage imageNamed:selectedImage];
    }
    
    [self addChildViewController:vc];
}
@end
